package com.beecloud.learning.charpter.one;

/**
 * Created with IntelliJ IDEA.
 * User: hengsun
 * Date: 3/23/17
 * Time: 3:47 PM
 * Description:
 */
public class UnsafeSequenceTest {
    public static void main(String[] args) {
        int threadNum = 100;

        Thread[] threads = new Thread[threadNum];
        for (int i =0 ; i < threadNum; i ++){
            UnsafeSequence unsafeSequence = new UnsafeSequence();
            new Thread(new UnsafeSequenceTestThread(unsafeSequence, i)).start();
        }
        //for (Thread t : threads) {
        //    t.start();
        //}

    }

    private static class UnsafeSequenceTestThread implements Runnable {
        private UnsafeSequence sequence;
        private int no;

        UnsafeSequenceTestThread(UnsafeSequence sequence, int no) {
            this.sequence = sequence;
            this.no = no;
        }

        @Override
        public void run() {
            for(int i = 0; i < 100; i ++) {
                int next = sequence.getNext();
                System.out.println(String.format("Thread no: %d, value:%d, time:%d", no, next, i));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

